const app = getApp();
Page({
    data: {
        //判断小程序的API，回调，参数，组件等是否在当前版本可用。
        appid:'wx4d3f39d47981353c',
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        openid:"",
        code:"",
        token:"",
        session_key:"",
        nickName:"",
        avatarUrl:"",
        province:"",
        city:"",
    },
    onLoad: function () {
        var that = this;
        wx.getStorage({
          key: 'token',
          success: (res) => {
                if( res.data != ""){
                    // console.log("已经有token:" +res.data);
                    wx.switchTab({ 
                        url: '/pages/index/index'  
                    })
                }else{//没有token才进行下一步
                    // 查看是否授权
                    wx.getSetting({ 
                        success: function (res) {
                            if (res.authSetting['scope.userInfo']) {
                                wx.getUserInfo({
                                    success: function (res) {
                                        //从数据库获取用户信息
                                        that.queryUsreInfo();
                                        //用户已经授权过
                                        wx.switchTab({
                                            url: '/pages/index/index'
                                        })
                                    }
                                });
                            }
                        }
                    })
                    this.queryUsreInfo();
                }
            }
        })
      
    },
    //    以上为生命周期onload函数
    //获取用户信息接口
    queryUsreInfo: function () {
        // console.log("被点击");
        var that = this;
        wx.login({
            success:  res => {
                that.setData({
                    code:res.code,
                })  
                let params = {
                    code: res.code,
                }
                console.log(this.data.code)
                wx.request({
                    url: 'http://api_dev.wanxikeji.cn/api/codeExchangeOpenid',
                    data: params,
                    success: (res) => {
                        wx.request({
                            url: 'http://api_dev.wanxikeji.cn/api/refreshToken',
                            data: {
                                openid:res.data.data.openid
                            },
                            success: (res) => {
                                console.log(res);
                                wx.setStorageSync('token', res.data.data.token);
                            } 
                        }); 
                        if(res.data.data.info.token != ""){
                            wx.setStorageSync('token', res.data.data.info.token)
                            wx.switchTab({
                                url: '/pages/index/index'  
                            })
                            this.setData({
                                openid:res.data.data.openid,
                                session_key:res.data.data.session_key,
                                token:res.data.data.info.token
                            })
                
                        }else{
                            console.log("这是当前获取的token!"+"-----"+res.data.data.info.token+"-----");
                        }
                       
                    } 
                });
            }
        });
    },
    bindGetUserInfo: function (e) {
        let myData = {
            name:e.detail.userInfo.nickName,
            url: e.detail.userInfo.avatarUrl,
        }
        wx.setStorageSync('my', myData);
        if (e.detail.userInfo) {
            //用户按了允许授权按钮
            var that = this; 
            // 插入登录的用户的相关信息到数据库
            wx.request({ 
                url: "http://api_dev.wanxikeji.cn/api/login",
                data: {
                    openid:this.data.openid,
                    nickName: e.detail.userInfo.nickName,
                    avatarUrl: e.detail.userInfo.avatarUrl,
                    province:e.detail.userInfo.province,
                    city: e.detail.userInfo.city
                },
                success: function (res) {
                    //从数据库获取用户信息
                    that.queryUsreInfo();
                    console.log("插入小程序登录用户信息成功！");
                }
            }); 
            wx.request({ 
                url: "http://api_dev.wanxikeji.cn/api/register",
                data: {
                    openid:this.data.openid,
                    nickName: e.detail.userInfo.nickName,
                    avatarUrl: e.detail.userInfo.avatarUrl,
                    province:e.detail.userInfo.province
                },
                success: (res) => {
                    that.queryUsreInfo();
                    // return
                    if( res.data.data.token != undefined || res.data.data.token  != '' ){
                        wx.setStorageSync('token', res.data.data.token);
                        this.setData({
                            token:res.data.data.token
                        })
                        wx.switchTab({
                            url: '/pages/index/index'  
                        })
                        that.queryUsreInfo();
                    }else{ 
                        console.log( "当前为异步请求，token返回需要一段时间");
                    }
                }
            }); 
        } else {
            //用户按了拒绝按钮
            wx.showModal({
                title:'警告',
                content:'您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
                showCancel:false,
                confirmText:'返回授权',
                success:function(res){
                    if (res.confirm) {
                        console.log('用户点击了“返回授权”')
                    } 
                }
            })
        }
    },
})